package com.udi.project.business.local.gs1.service.impl;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.udi.common.utils.StringUtils;
import com.udi.common.utils.uuid.IdUtils;
import com.udi.project.business.local.gs1.domain.BeUsable;
import com.udi.project.business.local.gs1.domain.Gs1Code;
import com.udi.project.business.local.gs1.domain.Gs1Data;
import com.udi.project.business.local.gs1.domain.SelGsMesVo;
import com.udi.project.business.local.gs1.mapper.Gs1GenerateMapper;
import com.udi.project.business.local.gs1.service.Gs1GenerateService;
import com.udi.project.business.local.gs1.thread.Gs1Thread;
import com.udi.project.business.utils.redis.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @author Admin
 * GS1生成业务层
 */
@Service
@Slf4j
public class Gs1GenerateServiceImpl implements Gs1GenerateService {

    /** redis工具类*/
    @Autowired
    private RedisUtils redisUtils;
    /** 数据库交互层*/
    @Autowired
    private Gs1GenerateMapper gs1GenerateMapper;



    /**
     * GS1生成核心业务[支持批量生成]
     * */
    @Override
    public int generate(BeUsable usable) {
        //获取DI码
        String diString = usable.getZxxsdycpbs();
        //创建udi集合
        List<Gs1Data> data = new ArrayList<>();
        //取得全部标识符信息
        List<Gs1Data> ai_infos = gs1GenerateMapper.getaiInfo();
        String qqmyxmdmCode = null,scph_code = null,scrq_code = null,sxrq_code = null,scxlh_code = null;
        for (Gs1Data ai_info : ai_infos) {
            if(ai_info.getAiInfoEn().equals("qqmyxmdm_code")){
                qqmyxmdmCode = ai_info.getAi();
            }
            if(ai_info.getAiInfoEn().equals("scrq_code")){
                scrq_code = ai_info.getAi();
            }
            if(ai_info.getAiInfoEn().equals("sxrq_code")){
                sxrq_code = ai_info.getAi();
            }
            if(ai_info.getAiInfoEn().equals("scph_code")){
                scph_code = ai_info.getAi();
            }
            if(ai_info.getAiInfoEn().equals("xlh_code")){
                scxlh_code = ai_info.getAi();
            }
        }
        //最小销售单元是否为空
        if(diString != null && diString.length() != 0){
            redisUtils.set("GS1key",usable.getCpmctymc());
            for(Integer i=1;i<usable.getNum()+1;i++){
                Gs1Data gs1Data = new Gs1Data();
                String produceDate = dateFormat(usable.getProduceDate());
                String validEndDate = dateFormat(usable.getProduceDate());
                //判断取得的数是否为空
                //生产日期
                if (StringUtils.isNotEmpty(produceDate)) {
                    gs1Data.setProduceDate(scrq_code + produceDate);
                } else {
                    gs1Data.setProduceDate("");
                }
                //失效日期
                if(StringUtils.isNotEmpty(validEndDate)){
                    gs1Data.setValidEndDate(sxrq_code+validEndDate);
                }else {
                    gs1Data.setValidEndDate("");
                };
                //生产批号
                if(StringUtils.isNotEmpty(usable.getBatchNum())){
                    gs1Data.setBatchNum(scph_code+usable.getBatchNum());
                }else {
                    gs1Data.setBatchNum("");
                };
                //生产序列号
                String serialNum = IdUtils.SimpleUUID();
                usable.setSerialNum(serialNum);
                String pi = gs1Data.getProduceDate()+gs1Data.getValidEndDate()+gs1Data.getBatchNum()+scxlh_code+serialNum;
                String GS1_128 = qqmyxmdmCode+diString+pi;
                //数据库信息插入
                gs1Data.setProduceDate(dateFormat(usable.getProduceDate()));
                gs1Data.setValidEndDate(dateFormat(usable.getValidEndDate()));
                gs1Data.setBatchNum(usable.getBatchNum());
                gs1Data.setSerialNum(serialNum);
                gs1Data.setDi(diString);
                gs1Data.setPi(pi);
                gs1Data.setUdi(GS1_128);
                gs1Data.setDelFlag("0");

                redisUtils.lSet(usable.getCpmctymc(),GS1_128);
                data.add(gs1Data);
            }
        }
        List<Gs1Code> gs1CodeList = new ArrayList<>();
        String key = (String) redisUtils.get("GS1key");
        List<Object> value = redisUtils.lGet(key,0,-1);
        for(Object va:value){
            Gs1Code code = new Gs1Code();
            code.setName(key);
            code.setCode((String) va);
            gs1CodeList.add(code);
        }
        redisUtils.del(key);

        redisUtils.SetObject("data",data);
        /** 持久化*/
        Gs1Thread gs1Thread = new Gs1Thread();
        gs1Thread.start();
        log.info("GS1-128码生成列表---》"+gs1CodeList.toString());
        return data.size();
    }

    @Override
    public int gsedit(List<SelGsMesVo> data) {
        return gs1GenerateMapper.gsedit(data);
    }

    @Override
    public int gsdel(List<SelGsMesVo> data) {
        return gs1GenerateMapper.gsdel(data);
    }

    /**
     * 格式化日期方法
     * @param date
     * @return
     */
    private static String dateFormat(Date date){
        if(date==null){
            return null;
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
            String de = sdf.format(date);
            return de.substring(2,8);
        }
    }

}
